from fastapi import FastAPI, Header, Depends
#from pydantic import BaseModel
from typing import Dict
from user import userLogin, userInfo, userLogout, generateToken, certifyToken
from register import getRegisterList, setRegister
from base import getHosp, mediUnitList, mediPassList, useTypeList, diagList, chargeCatList
from drug import getDrugList
from pres import savePres, getPresOutList, getPresDetail
from starlette.staticfiles import StaticFiles
from starlette.responses import HTMLResponse
from zjyb import yb_7101, yb_7104  # 医保电子处方上传7101，撤消7104

app = FastAPI()


app.mount("/static", StaticFiles(directory="../dist/static"), name="static")


@app.get("/")
def read_root():
    with open('../dist/index.html', encoding='utf-8') as f:
        return HTMLResponse(content=f.read(), status_code=200)

@app.get("/test")
def test():
    data = { 'pres_id': 433, 'patient_id': 230, 'patient_age': 40 }
    fields = { 'pres_id' :'pres_id', 'patient_id': 'patient_id','age': 'patient_age', 'sex': 'patient_sex'}
    return mapData(data, fields)

#######user.py#######
@app.post("/user/login")
def user_login(loginData: Dict):
    return userLogin(loginData)

# 依赖函数
def certify(x_token: str=Header(None)):
    return certifyToken(x_token)

@app.get("/user/info")
def user_info(ret=Depends(certify)):
    if ret.get('result'):
        return userInfo(ret.get('username'))
    else:
        return ret.get('ret')

@app.post("/user/logout")
def user_logout(x_token: str=Header(None), ret=Depends(certify)):
    if ret.get('result'):
        return userLogout(ret.get('username'), x_token)
    else:
        return ret.get('ret')

######register.py#######
@app.get("/register/getList")
def register_get_list(ret=Depends(certify)):
    if ret.get('result'):
        info = userInfo(ret.get('username'))
        doct_id = info['data']['userInfo']['staff_id']
        sect_id = info['data']['userInfo']['sect_id']
        return getRegisterList(doct_id, sect_id)
    else:
        return ret.get('ret')

@app.post("/register/setRegister")
def set_register(data: Dict, ret=Depends(certify)):
    if not ret.get('result'):
        return ret.get('ret')
    else:
        return setRegister(data)

##### base.py ######
@app.get("/base/getHosp")
def get_hosp():
    return getHosp()

@app.get("/base/mediUnitList")
def medi_unit_list(ret=Depends(certify)):
    if not ret.get('result'):
        return ret.get('ret')
    else:
        return mediUnitList()

@app.get("/base/mediPassList")
def medi_pass_list(ret=Depends(certify)):
    if not ret.get('result'):
        return ret.get('ret')
    else:
        return mediPassList()

@app.get("/base/useTypeList")
def use_type_list(ret=Depends(certify)):
    if not ret.get('result'):
        return ret.get('ret')
    else:
        return useTypeList()

@app.post("/base/diagList")
def diag_list(data: Dict, ret=Depends(certify)):
    if not ret.get('result'):
        return ret.get('ret')
    else:
        return diagList(data)

@app.get("/base/chargeCatList")
def charge_cat_list(ret=Depends(certify)):
    if not ret.get('result'):
        return ret.get('ret')
    else:
        return chargeCatList()

# 药品
@app.get("/drug/getDrugList")
def get_drug_list(keyword: str, ret=Depends(certify)):
    if not ret.get('result'):
        return ret.get('ret')
    else:
        return getDrugList(keyword)

# 处方
@app.post("/pres/savePres")
def save_pres(data: Dict, ret=Depends(certify)):
    if not ret.get('result'):
        return ret.get('ret')
    else:
        return savePres(data)
    
@app.post("/pres/getPresOutList")
def get_pres_outist(data: Dict, ret=Depends(certify)):
    if not ret.get('result'):
        return ret.get('ret')
    else:
        return getPresOutList(data)

@app.get("/pres/getPresDetail")
def get_pres_detail(pres_id: str, ret=Depends(certify)):
    if not ret.get('result'):
        return ret.get('ret')
    else:
        return getPresDetail(pres_id)

# 医保接口（电子处方上传）
@app.get("/zjyb/7101")
def medi7101(pres_id: str, x_token: str=Header(None), ret=Depends(certify)):
    if not ret.get('result'):
        return ret.get('ret')
    else:
        return yb_7101(pres_id, x_token)
        
# 医保接口（电子处方撤消）
@app.post("/zjyb/7104")
def medi7104(data:Dict, x_token: str=Header(None), ret=Depends(certify)):
    if not ret.get('result'):
        return ret.get('ret')
    else:
        return yb_7104(data, x_token)

# end    